package com.cgx.controller;

import com.cgx.webmagic.MyPageProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import us.codecraft.webmagic.Spider;

import java.util.HashMap;
import java.util.Map;

/**
 * @author chenguanxin
 * @date 2020-07-18 17:20
 */
@Controller
@RequestMapping("/webMagic")
public class WebMagicController {

    @Autowired
    MyPageProcessor myPageProcessor;

    @RequestMapping("/start")
    @ResponseBody
    public Object start() {
        Map<String, Object> result = new HashMap<>();
        try {
            Spider.create(myPageProcessor).addUrl("http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2019/index.html").start();
            result.put("msg", "开始成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.put("msg", "开始失败");
        }
        return  result;
    }
}
